package com.koi.classservice.Utils;

import com.koi.classservice.domain.po.Course;
import com.koi.classservice.domain.po.CourseSchedule;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 数据解析工具类
@Component
public class DataParser {
    private static final String[] DAYS = {"一", "二", "三", "四", "五"};
    private static final String[] SESSIONS = {"1-2", "3-4", "5-6", "7-8", "9-10", "11-12"};

    public static List<CourseSchedule> parseCourses(List<Course> courses) {
        List<CourseSchedule> scheduleList = new ArrayList<>();
        for (Course course : courses) {
            String weekStr = course.getWeek();
            String sessionStr = course.getSession();
            String classroom = course.getClassroom();


            String day = parseDay(sessionStr);
            List<Integer> weeks = parseWeeks(weekStr, sessionStr);
            String session = parseSessionRange(sessionStr);
            Boolean isSingle = parseIsSingle(sessionStr);

            for (Integer week : weeks) {
                CourseSchedule schedule = new CourseSchedule();
                schedule.setWeek(String.valueOf(week));//有修改
                schedule.setDay(day);
                schedule.setClassTime(session);
                schedule.setClassroom(classroom);
                schedule.setIsSingle(isSingle);
                scheduleList.add(schedule);
            }
        }
        return scheduleList;
    }

    private static List<Integer> parseWeeks(String weekStr, String sessionStr) {
        List<Integer> weeks = new ArrayList<>();
        String[] ranges = weekStr.split(",");
        boolean isSingle = sessionStr.contains("单");
        boolean isDouble = sessionStr.contains("双");

        for (String range : ranges) {
            if (range.contains("-")) {
                String[] parts = range.split("-");
                int start = Integer.parseInt(parts[0]);
                int end = Integer.parseInt(parts[1]);
                if (isSingle) {
                    for (int i = start; i <= end; i += 2) {
                        weeks.add(i);
                    }
                } else if (isDouble) {
                    for (int i = start + (start % 2 == 0 ? 0 : 1); i <= end; i += 2) {
                        weeks.add(i);
                    }
                } else {
                    for (int i = start; i <= end; i++) {
                        weeks.add(i);
                    }
                }
            } else {
                int week = Integer.parseInt(range);
                if ((!isSingle && !isDouble) ||
                        (isSingle && week % 2 == 1) ||
                        (isDouble && week % 2 == 0)) {
                    weeks.add(week);
                }
            }
        }
        return weeks;
    }

    private static String parseDay(String sessionStr) {
        Pattern pattern = Pattern.compile("([一二三四五])");
        Matcher matcher = pattern.matcher(sessionStr);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    private static String parseSessionRange(String sessionStr) {
        Pattern pattern = Pattern.compile("\\[([0-9\\-]+)节\\]");
        Matcher matcher = pattern.matcher(sessionStr);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    private static Boolean parseIsSingle(String sessionStr) {
        if (sessionStr.contains("单")) {
            return true;
        } else if (sessionStr.contains("双")) {
            return false;
        }
        return null;
    }
}